package com.yunshan.member.service.impl;


import com.yunshan.common.utils.DateUtils;
import com.yunshan.common.utils.StringUtils;
import com.yunshan.common.utils.uuid.UUID;
import com.yunshan.member.domain.MemberUserInfo;
import com.yunshan.member.domain.ResultMember;
import com.yunshan.member.mapper.MemberUserInfoMapper;
import com.yunshan.member.service.IMemberSubCardInfoService;
import com.yunshan.member.service.IMemberUserInfoService;
import com.yunshan.wechat.domain.UserInfoVo;
import com.yunshan.wechat.domain.WechatUserInfo;
import com.yunshan.wechat.service.IWechatUserInfoService;
import com.yunshan.welfare.domain.WelfareCouponRecord;
import com.yunshan.welfare.domain.WelfareGiftRecord;
import com.yunshan.welfare.service.IWelfareCouponRecordService;
import com.yunshan.welfare.service.IWelfareGiftRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;

/**
 * 会员信息Service业务层处理
 *
 * @author yunshan
 * @date 2025-01-17
 */
@Service
public class MemberUserInfoServiceImpl implements IMemberUserInfoService
{
    @Autowired
    private MemberUserInfoMapper memberUserInfoMapper;
    @Autowired
    private IWechatUserInfoService wechatUserInfoService;
    @Autowired
    private IMemberSubCardInfoService memberSubCardInfoService;
    @Autowired
    private IWelfareGiftRecordService welfareGiftRecordService;
    @Autowired
    private IWelfareCouponRecordService welfareCouponRecordService;

    /**
     * 查询会员信息
     *
     * @param id 会员信息主键
     * @return 会员信息
     */
    @Override
    public MemberUserInfo selectMemberUserInfoById(Long id)
    {
        MemberUserInfo memberUserInfo = memberUserInfoMapper.selectMemberUserInfoById(id);
        //查询小程序用户信息
        if (memberUserInfo == null) {
            return null;
        }

        if (memberUserInfo.getUserId() != null) {
            WechatUserInfo wechatUserInfo = wechatUserInfoService.selectWechatUserInfoById(memberUserInfo.getUserId());
            if (wechatUserInfo != null) {
                memberUserInfo.setSex(wechatUserInfo.getSex());
                memberUserInfo.setBirthday(wechatUserInfo.getBirthday());
            }
        }
        //查询礼包数
        WelfareGiftRecord welfareGiftRecord = new WelfareGiftRecord();
        welfareGiftRecord.setMemberId(id);
        memberUserInfo.setGiftCount(welfareGiftRecordService.selectWelfareGiftRecordListGroup(welfareGiftRecord).size());

        //查询优惠券数量
        WelfareCouponRecord welfareCouponRecord = new WelfareCouponRecord();
        welfareCouponRecord.setMemberId(id);
        memberUserInfo.setCouponCount(welfareCouponRecordService.selectWelfareCouponRecordCount(welfareCouponRecord));

        //查询副卡张数
        memberUserInfo.setSubCount(memberSubCardInfoService.selectCountByMemberId(id));
        return memberUserInfo;
    }

    /**
     * 查询会员信息列表
     *
     * @param memberUserInfo 会员信息
     * @return 会员信息
     */
    @Override
    public List<MemberUserInfo> selectMemberUserInfoList(MemberUserInfo memberUserInfo)
    {
        return memberUserInfoMapper.selectMemberUserInfoList(memberUserInfo);
    }

    /**
     * 新增会员信息
     *
     * @param memberUserInfo 会员信息
     * @return 结果
     */
    @Override
    public MemberUserInfo insertMemberUserInfo(MemberUserInfo memberUserInfo)
    {
        MemberUserInfo memberUser = memberUserInfoMapper.selectByLoginPhone(memberUserInfo.getPhone());
        //查询小程序是否注册，通过手机号绑定小程序
        Long userId = memberUserInfoMapper.selectWeChatUserId(memberUserInfo.getPhone());
        if (userId != null){
            memberUserInfo.setUserId(userId);
            memberUserInfo.setStatus(0);

            //更新小程序用户名
            if (StringUtils.isNotEmpty(memberUserInfo.getName())) {
                WechatUserInfo wechatUserInfo = new WechatUserInfo();
                wechatUserInfo.setId(userId);
                wechatUserInfo.setName(memberUserInfo.getName());
                wechatUserInfoService.updateWechatUserInfo(wechatUserInfo);
            }
        }

        //生成会籍号
        int i = UUID.getSecureRandom().nextInt(999999999) + 100000000;
        memberUserInfo.setMemberNum(Long.parseLong(memberUserInfo.getMemberType() + "00" + i));
        memberUserInfo.setIssueTime(DateUtils.getNowDate());

        if (memberUser == null){
            memberUserInfo.setLoginPhone(memberUserInfo.getPhone());
            memberUserInfo.setCreateTime(DateUtils.getNowDate());
            int j = memberUserInfoMapper.insertMemberUserInfo(memberUserInfo);

            if (j > 0){
                return memberUserInfoMapper.selectMemberUserInfoByMemberNum(memberUserInfo.getMemberNum());
            }

            //如果存在临时会籍，则更新
        }else {
            memberUserInfo.setId(memberUser.getId());
            int j = memberUserInfoMapper.updateMemberUserInfo(memberUserInfo);
            if (j > 0){
                return memberUserInfoMapper.selectMemberUserInfoByMemberNum(memberUserInfo.getMemberNum());
            }
        }
        return null;
    }

    /**
     * 修改会员信息
     *
     * @param memberUserInfo 会员信息
     * @return 结果
     */
    @Override
    public int updateMemberUserInfo(MemberUserInfo memberUserInfo)
    {
        //如果更新了名字小程序用户名同步更新
        if (StringUtils.isNotEmpty(memberUserInfo.getName())){
            MemberUserInfo memberUser = memberUserInfoMapper.selectMemberUserInfoById(memberUserInfo.getId());
            if (memberUser != null && memberUser.getUserId() != null){
                WechatUserInfo wechatUserInfo = new WechatUserInfo();
                wechatUserInfo.setId(memberUser.getUserId());
                wechatUserInfo.setName(memberUserInfo.getName());
                wechatUserInfoService.updateWechatUserInfo(wechatUserInfo);
            }
        }

        memberUserInfo.setUpdateTime(DateUtils.getNowDate());
        return memberUserInfoMapper.updateMemberUserInfo(memberUserInfo);
    }

    /**
     * 批量删除会员信息
     *
     * @param ids 需要删除的会员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberUserInfoByIds(Long[] ids)
    {
        return memberUserInfoMapper.deleteMemberUserInfoByIds(ids);
    }

    /**
     * 删除会员信息信息
     *
     * @param id 会员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberUserInfoById(Long id)
    {
        return memberUserInfoMapper.deleteMemberUserInfoById(id);
    }

    @Override
    public MemberUserInfo selectMemberUserInfoByLoginPhone(String loginPhone) {
        return memberUserInfoMapper.selectByLoginPhone(loginPhone);
    }

    @Override
    public int updateMemberBalance(Long id, BigDecimal amount) {
        return memberUserInfoMapper.updateMemberBalance(id,amount);
    }

    @Override
    public ResultMember selectResultMember(Long memberId) {
        return memberUserInfoMapper.selectResultMember(memberId);
    }

    @Override
    public int addMemberBalance(Long id, BigDecimal amount) {
        return memberUserInfoMapper.addMemberBalance(id,amount);
    }

    @Override
    public UserInfoVo selectUniUserInfo(Long userId) {
        return memberUserInfoMapper.selectUniUserInfo(userId);
    }

    @Override
    public MemberUserInfo selectMemberUserInfoByPhone(String phone) {
        return memberUserInfoMapper.selectByPhone(phone);
    }

    @Override
    public MemberUserInfo selectUserInfoByUserId(Long userId) {
        return memberUserInfoMapper.selectUserInfoByUserId(userId);
    }

    @Override
    public int verifyGift(Long id) {
        return memberUserInfoMapper.verifyGift(id);
    }
}
